/************************************************************************/
/*																		*/
/*		School Hangover Project											*/
/*																		*/
/*		IMAC 2010														*/
/*		Author : 	Helena Duchaussoy									*/
/*					Isabelle Gouwy										*/
/*					Nathalie Kuoch										*/
/*					Franck Letellier									*/
/*					Louise Pequignot									*/
/*																		*/
/*		E-mail: flo-in-underware@googlegroups.com						*/
/*																		*/
/************************************************************************/

#include "Stage.h"

#include "Player.h"
#include "Room.h"


Stage::Stage(const std::string& sname){
	m_sname = sname;

	m_proot = new osg::Group();

	m_vroom.push_back(new Room("../data/script/room1.lua", "room_1.osg"));
	m_vroom.push_back(new Room("../data/script/room2.lua", "room_2.osg"));
	m_vroom.push_back(new Room("../data/script/room3.lua", "room_3.osg"));
	m_vroom.push_back(new Room("../data/script/room4.lua", "room_4.osg"));
	m_vroom.push_back(new Room("../data/script/room5.lua", "room_5.osg"));
	m_vroom.push_back(new Room("../data/script/room6.lua", "room_6.osg"));
	m_vroom.push_back(new Room("../data/script/room7.lua", "room_7.osg"));
	m_vroom.push_back(new Room("../data/script/room8.lua", "room_8.osg"));
	m_vroom.push_back(new Room("../data/script/room9.lua", "room_9.osg"));
	m_vroom.push_back(new Room("../data/script/room10.lua", "room_10.osg"));
	m_vroom.push_back(new Room("../data/script/room11.lua", "room_11.osg"));
	m_vroom.push_back(new Room("../data/script/corridor.lua", "corridor.osg"));

	osg::Node * floor = osgDB::readNodeFile("../data/mesh/floor.osg");
	//corridor = osgDB::readNodeFile("../data/mesh/corridor.osg");
	//ComputeBoundingBox b2;
    //corridor->accept(b2);
	m_proot->addChild(floor);
	//m_proot->addChild(corridor);	
	m_phud = new Hud();
	m_pplayer = new Player();

	m_proot->addChild(m_phud->getCamHud());
}

Stage::~Stage() {
	m_vroom.clear();
//	delete m_proot;
	delete m_pplayer;
	delete m_phud;

	m_proot = NULL;
	m_pplayer = NULL;
}

void Stage::init() {
	for(size_t i=0; i< m_vroom.size(); i++) {
		m_vroom[i]->init();
	}
	Engine::getInstance().getTimer()->start();
	m_pplayer->init();

	Engine::getInstance().getTimer()->start();
	///Set the name of the next stage 
	m_snextStage = "mainStage";
}

void Stage::update(){
	for(size_t i=0; i< m_vroom.size(); i++) {
		m_vroom[i]->update();
	}
	Engine::getInstance().getTimer()->update();
	m_phud->update();
	m_pplayer->update();	
}

bool Stage::isFinished(){

	return false;
}

const osg::Vec3d&  Stage::getPlayerPos(){
	
	return m_pplayer->getPosition();

}

bool Stage::computeCollision(Player* player){
	ComputeBoundingBox b =(*player).getBoundBox();
	osg::Node * n;
	std::vector<osg::BoundingBox> v_bbox ;
	
	// Walls collisions
	/*for(size_t i=0; i< m_vroom.size(); i++) {
		v_bbox = m_vroom[i]->getWall()->getBoundBoxs();
		for (unsigned int i=0; i< v_bbox.size() ; i++) {
			if (b.getBoundBox().intersects(v_bbox.at(i))) return true;
		}*/
	/*	getObjects()
		corridor->accept(b2);
		v_bbox = b2.getBoundBoxs();
		for (unsigned int i=0; i< v_bbox.size() ; i++) {
			if (b.getBoundBox().intersects(v_bbox.at(i))) return true;
		}
		for (unsigned int i=0; i< v_bbox.size() ; i++) {
			if (b.getBoundBox().intersects(v_bbox.at(i))) return true;
		}
		*/
	//}
	
	// Corridor collisons
/*	ComputeBoundingBox b2;
    corridor->accept(b2);
    v_bbox = b2.getBoundBoxs();
	for (unsigned int i=0; i< v_bbox.size() ; i++) {
		if (b.getBoundBox().intersects(v_bbox.at(i))) return true;
	}
*/	

	if(m_proot != NULL) {
		for (unsigned int i=0; i< (*m_proot).getNumChildren()-1 ; i++) {
			ComputeBoundingBox b2;
			n = (*m_proot).getChild(i);
			if(n != NULL) {
				n->accept(b2);
				v_bbox = b2.getBoundBoxs();
				for (unsigned int j=0; j< v_bbox.size() ; j++) {
					if (b.getBoundBox().intersects(v_bbox.at(j))) return true;
				}
			}
		}
	}
	// Objects collisions
	
   /*std::cout << "Bbox-player : x(" << b.getBoundBox().xMin() << " - " << b.getBoundBox().xMax() 
			<< ") y(" << b.getBoundBox().yMin() << " - " << b.getBoundBox().yMax() 
			<< ") z("  << b.getBoundBox().zMin() << " - " << b.getBoundBox().zMax() <<  ")" << std::endl;
    std::cout << "Bbox-"<<(*n).getName()<< ": x(" << b2.getBoundBox().xMin() << " - " << round(b2.getBoundBox().xMax())
			<< ") y(" << round(b2.getBoundBox().yMin()) << " - "  << round(b2.getBoundBox().yMax())
			<< ") z("  << round(b2.getBoundBox().zMin()) << " - " << round(b2.getBoundBox().zMax()) <<  ")" << std::endl;*/
   // if (b.getBoundBox().intersects(b2.getBoundBox())) std::cout << " BOOM " << std::endl;

   return false;
}


bool Stage::computeInteraction(Player* player){ 
	std::vector<Room*>::iterator iter;
	iter = m_vroom.begin();
	while (iter != m_vroom.end()){
		if((*iter)!= NULL){
			if((*iter)->computeInteraction(player)){
				return true;
			}
			++iter;
		}
	}
	
   return false;
}
